package www.rain.com.test.controller;

import java.net.BindException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import www.rain.com.test.bean.LoginInfo;


@SuppressWarnings("deprecation")
public class LoginController extends SimpleFormController{

	private String failView;
	private String okView;

	protected ModelAndView onSubmit(Object cmd)throws Exception {

		LoginInfo loginInfo = (LoginInfo) cmd;

		System.out.println("--12312313-- name="+loginInfo.getUsername());
		if (login(loginInfo) == 0) {

			Map result_map = new HashMap();
			result_map.put("logininfo", loginInfo);

			List msgList = new LinkedList();
			msgList.add("msg1");
			msgList.add("msg2");
			msgList.add("msg3");
			result_map.put("messages", msgList);

			return new ModelAndView(this.getOkView(), result_map);
		} else {
			return new ModelAndView(this.getFailView());
		}

	}

	private int login(LoginInfo loginInfo) {

		if ("admin".equalsIgnoreCase(loginInfo.getUsername())
				&& "admin".equals(loginInfo.getPassword())) {
			return 0;
		}
		return 1;
	}

	public String getFailView() {
		return failView;
	}

	public void setFailView(String failView) {
		this.failView = failView;
	}

	public String getOkView() {
		return okView;
	}

	public void setOkView(String okView) {
		this.okView = okView;
	}

}
